package cc.zzone.domain;

import cc.zzone.util.SpringUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.ibatis.annotations.*;

import java.io.Serial;
import java.io.Serializable;
import java.util.List;

/**
 * 文件上传对象
 *
 * @author jinzhengze
 * @since 2023年10月15日
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class FileRes implements Serializable {

    @Serial
    private static final long serialVersionUID = 22L;

    private String id;

    private String name;

    private byte[] file;

    public static FileResMapper mapper() {
        return SpringUtil.getBean(FileResMapper.class);
    }

    @Mapper
    public interface FileResMapper {

        @Select("select * from `file_res` where `id` = #{id}")
        @Results(id = "result", value = {
                @Result(column = "id", property = "id", id = true),
                @Result(column = "name", property = "name"),
                @Result(column = "file", property = "file")})
        FileRes getData(@Param("id") String id);

        @Select({"<script>",
                "select `id`, `name` from `file_res`",
                "<where>",
                "<if test=\"id != null\">and `id` = #{id}</if>",
                "<if test=\"name != null and name != ''\">and `name` = #{name}</if>",
                "</where>",
                "</script>"})
        @ResultMap("result")
        List<FileRes> listData(FileRes data);

        @Select({"<script>",
                "select `id`, `name` from `file_res`",
                "<where>",
                "<if test=\"id != null\">and `id` like concat(#{id}, '%')</if>",
                "<if test=\"name != null and name != ''\">and `name` like concat(#{name}, '%')</if>",
                "</where>",
                "</script>"})
        @ResultMap("result")
        List<FileRes> likeData(FileRes data);

        @Insert({"<script>",
                "insert into `file_res`",
                "(",
                "<if test=\"id != null and id != ''\">`id`,</if>",
                "<if test=\"name != null and name != ''\">`name`,</if>",
                "`file`) values (",
                "<if test=\"id != null and id != ''\">#{id},</if>",
                "<if test=\"name != null and name != ''\">#{name},</if>",
                "#{file})",
                "</script>"})
        Integer insertData(FileRes data);

        @Update({"<script>",
                "update `file_res`",
                "<set>",
                "<if test=\"name != null and name != ''\">`name` = #{name},</if>",
                "<if test=\"file != null\">`file` = #{file},</if>",
                "</set>",
                "where `id` = #{id}",
                "</script>"})
        Integer updateData(FileRes data);

        @Delete({"<script>",
                "delete from `file_res` where id in",
                "<foreach item='id' collection='array' open='(' separator=',' close=')'>",
                "#{id}",
                "</foreach>",
                "</script>"})
        Integer deleteData(String... ids);
    }
}
